package cn.edu.sdjzu.xg23.kcsj01_backend.controller;

import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Textbook;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.TextbookService;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/jcgl")
public class TextbookController {

    @Autowired
    private TextbookService textbookService;

    @PostMapping("/Textbook")
    @PreAuthorize("hasAuthority('textbook:add')")
    public Result add(@RequestBody Textbook textbook) {
        textbookService.save(textbook);
        return Result.success("新增成功");
    }

    @DeleteMapping("/textbook/{id}")
    @PreAuthorize("hasAuthority('textbook:delete')")
    public Result delete(@PathVariable Long id) {
        textbookService.deleteById(id);
        return Result.success("删除成功");
    }

    @PutMapping("/textbook")
    @PreAuthorize("hasAuthority('textbook:update')")
    public Result update(@RequestBody Textbook textbook) {
        textbookService.update(textbook);
        return Result.success("修改成功");
    }

    // 修改后的 ProjectController.java 第37行开始的代码
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('textbook:search:all') or hasAuthority('textbook:search:my')")
    public Result selectTextbooksByCondition(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody(required = false) Textbook textbook,
            @RequestParam(required = false) Long employeeId) {
        PageInfo<Textbook> pageInfo = textbookService.selectTextbooksByCondition(pageNum, pageSize, textbook,employeeId);
        return Result.success(pageInfo);
    }
    @PreAuthorize("hasAuthority('textbook:approve')")
    @PutMapping("/approve")
    public Result approveTextbook(@RequestBody Textbook textbook) {
        if (textbookService.approveTextbook(textbook)) {
            return Result.success("审批成功");
        } else {
            return Result.error("500", "审批失败");
        }
    }
}

